package global

import (
	"github.com/gin-gonic/gin"
	"github.com/spf13/viper"
	"log"
)

type config struct {
	SessionKey string
	Port       string
}

var GlobalConfig *config

const (
	defaultPort = "8080"
)

func loadConfig() *config {
	config := &config{}
	viper.SetConfigName("config")
	viper.AddConfigPath(".")
	if err := viper.ReadInConfig(); err != nil {
		log.Fatal(err)
	}
	if err := viper.Unmarshal(config); err != nil {
		log.Fatal(err)
	}
	return config
}

func Load() gin.HandlerFunc {
	return func(c *gin.Context) {
		GlobalConfig = loadConfig()
	}
}
